package com.lanou.zy.projecta.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lanou.zy.projecta.R;
import com.lanou.zy.projecta.adapter.CheapTravelListViewAdapter;
import com.lanou.zy.projecta.adapter.CountryGridViewAdapter;
import com.lanou.zy.projecta.adapter.CountryViewPagerAdapter;
import com.lanou.zy.projecta.application.BaseApplication;
import com.lanou.zy.projecta.entity.CountryMsg;
import com.lanou.zy.projecta.entity.OtherCountry;
import com.lanou.zy.projecta.util.GridViewCountry;
import com.lanou.zy.projecta.util.MyListView;
import com.lanou.zy.projecta.util.ProgressView.CircleProgress;

import java.util.List;

/**
 * Created by dllo on 15/10/28.
 */
public class CountryActivity extends FragmentActivity implements AdapterView.OnItemClickListener, View.OnClickListener {
    private GridViewCountry countryGridView;
    private CountryGridViewAdapter countryGridViewAdapter;
    private RequestQueue queue;
    private ImageView countryBack_Iv;
    private TextView countryMsgContentTv, countryCnnNameTV, countryEnnNameTV;
    private BaseApplication app;

    private ViewPager countryViewPager;
    private CountryViewPagerAdapter countryViewPagerAdapter;

    private MyListView cheapTravelListView;
    private CheapTravelListViewAdapter cheapTravelListViewAdapter;
    private CircleProgress countryProgressView;
    private RelativeLayout country_Rv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_country);

        initView();
        initData();
    }

    private void initView() {
        app = (BaseApplication) getApplication();

        countryProgressView = (CircleProgress) findViewById(R.id.countryProgressView);
        country_Rv = (RelativeLayout) findViewById(R.id.country_Rv);
        countryProgressView.startAnim();

        countryBack_Iv = (ImageView) findViewById(R.id.countryBack_Iv);
        countryBack_Iv.setOnClickListener(this);
        countryMsgContentTv = (TextView) findViewById(R.id.countryMsgContentTv);
        countryCnnNameTV = (TextView) findViewById(R.id.countryCnnNameTV);
        countryEnnNameTV = (TextView) findViewById(R.id.countryEnnNameTV);

        countryGridView = (GridViewCountry) findViewById(R.id.countryGridView);
        countryGridView.setOnItemClickListener(this);
        countryGridViewAdapter = new CountryGridViewAdapter(this);
        countryGridView.setAdapter(countryGridViewAdapter);

        cheapTravelListView = (MyListView) findViewById(R.id.cheapTravelListView);
        View header = getLayoutInflater().inflate(R.layout.cheapreavel_listview_header, null);
        cheapTravelListView.addHeaderView(header);
        cheapTravelListViewAdapter = new CheapTravelListViewAdapter(this);
        cheapTravelListView.setAdapter(cheapTravelListViewAdapter);

        countryViewPager = (ViewPager) findViewById(R.id.countryViewPager);
        countryViewPagerAdapter = new CountryViewPagerAdapter(getSupportFragmentManager());
        countryViewPager.setAdapter(countryViewPagerAdapter);
    }

    private void initData() {
        queue = Volley.newRequestQueue(this);
        Intent intent = getIntent();
        String id = intent.getStringExtra("id");
        String type = intent.getStringExtra("type");
        if (type.equals("旅行地")) {
            app.setType(true);
            String url = "http://open.qyer.com/qyer/footprint/city_detail?client_id=qyer_android&client_secret=9fcaae8aefc4f9ac4915&v=1&track_deviceid=863811018458956&track_app_version=6.8.1&track_app_channel=taobao&track_device_info=msm8960&track_os=Android4.2.2&app_installtime=1445235475313&lat=38.883623&lon=121.544767&city_id=" + id;
            StringRequest request = new StringRequest(url, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Gson gson = new Gson();
                    OtherCountry o = gson.fromJson(response, OtherCountry.class);
                    OtherCountry.DataEntity od = o.getData();
                    List<String> odh = od.getPhotos();
                    List<OtherCountry.DataEntity.LocalDiscountEntity> odl = od.getLocal_discount();
                    List<OtherCountry.DataEntity.NewDiscountEntity> odn = od.getNew_discount();
                    countryGridViewAdapter.addDatas(odl);
                    cheapTravelListViewAdapter.addDatas(odn);
                    countryViewPagerAdapter.addData(odh);

                    countryMsgContentTv.setText(od.getEntryCont());
                    countryCnnNameTV.setText(od.getCnname());
                    countryEnnNameTV.setText(od.getEnname());

                    country_Rv.setVisibility(View.VISIBLE);
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    error.printStackTrace();
                }
            });
            queue.add(request);
        } else {
            app.setType(false);
            String url = "http://open.qyer.com/qyer/footprint/country_detail?client_id=qyer_android&client_secret=9fcaae8aefc4f9ac4915&v=1&track_deviceid=863811018458956&track_app_version=6.8.1&track_app_channel=taobao&track_device_info=msm8960&track_os=Android4.2.2&app_installtime=1445235475313&lat=38.883633&lon=121.544661&country_id=" + id;
            StringRequest request = new StringRequest(url, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Gson gson = new Gson();
                    CountryMsg c = gson.fromJson(response, CountryMsg.class);
                    CountryMsg.DataEntity cd = c.getData();
                    List<CountryMsg.DataEntity.HotCityEntity> cdc = cd.getHot_city();
                    List<CountryMsg.DataEntity.NewDiscountEntity> cdn = cd.getNew_discount();
                    List<String> photos = cd.getPhotos();
                    countryGridViewAdapter.addData(cdc);
                    cheapTravelListViewAdapter.addData(cdn);
                    countryViewPagerAdapter.addData(photos);

                    countryMsgContentTv.setText(cd.getEntryCont());
                    countryCnnNameTV.setText(cd.getCnname());
                    countryEnnNameTV.setText(cd.getEnname());

                    country_Rv.setVisibility(View.GONE);

                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    error.printStackTrace();
                }
            });
            queue.add(request);
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.countryBack_Iv:
                onBackPressed();
                break;
        }
    }
}
